/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.mahasen.client.gui;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import javax.swing.*;
import java.util.List;
import javax.swing.table.DefaultTableModel;

public class Property extends javax.swing.JFrame {

    List<NameValuePair> propeties;

    /** Creates new form Property */
    public Property() {
        initComponents();

    }

    /**
     *
     * @param propeties
     */
    public Property(List<NameValuePair> propeties) {
        initComponents();
        this.propeties = propeties;
//        addedPropertTablePanel.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        addCustomPropertyNameTextField = new javax.swing.JTextField();
        addCustomPropertValueTextField = new javax.swing.JTextField();
        addAnotherProepertyButton = new javax.swing.JButton();
        addCustomProeprtyOkButton = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        addedPropertTablePanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        addedPropertyTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Custom Property");

        jLabel1.setText("Name");

        jLabel2.setText("Value");

        addCustomPropertyNameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCustomPropertyNameTextFieldActionPerformed(evt);
            }
        });

        addAnotherProepertyButton.setText("Add Another");
        addAnotherProepertyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAnotherProepertyButtonActionPerformed(evt);
            }
        });

        addCustomProeprtyOkButton.setText("Ok");
        addCustomProeprtyOkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCustomProeprtyOkButtonActionPerformed(evt);
            }
        });

        jButton3.setText("Cancel");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        addedPropertyTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Property Name", "Property Value"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(addedPropertyTable);
        addedPropertyTable.getColumnModel().getColumn(0).setResizable(false);
        addedPropertyTable.getColumnModel().getColumn(1).setResizable(false);

        javax.swing.GroupLayout addedPropertTablePanelLayout = new javax.swing.GroupLayout(addedPropertTablePanel);
        addedPropertTablePanel.setLayout(addedPropertTablePanelLayout);
        addedPropertTablePanelLayout.setHorizontalGroup(
            addedPropertTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addedPropertTablePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );
        addedPropertTablePanelLayout.setVerticalGroup(
            addedPropertTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addedPropertTablePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(95, 95, 95)
                .addComponent(addAnotherProepertyButton)
                .addGap(26, 26, 26)
                .addComponent(addCustomProeprtyOkButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(54, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(addCustomPropertyNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(addCustomPropertValueTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(jLabel2)))
                .addGap(29, 29, 29))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(35, Short.MAX_VALUE)
                .addComponent(addedPropertTablePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addCustomPropertyNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addCustomPropertValueTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addedPropertTablePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addAnotherProepertyButton)
                    .addComponent(addCustomProeprtyOkButton)
                    .addComponent(jButton3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-448)/2, (screenSize.height-309)/2, 448, 309);
    }// </editor-fold>//GEN-END:initComponents

    private void addCustomPropertyNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCustomPropertyNameTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_addCustomPropertyNameTextFieldActionPerformed

    private void addAnotherProepertyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAnotherProepertyButtonActionPerformed
        String propertyName = addCustomPropertyNameTextField.getText();
        String propertyValue = addCustomPropertValueTextField.getText();

        if (propertyName.equals("") || propertyValue.equals("")) {
            JOptionPane.showMessageDialog(this, "Please enter both property name and value");
        }

        if (!propertyName.equals("") && !propertyValue.equals("")) {
            if (!propertyName.equals("fileName")) {
                propeties.add(new BasicNameValuePair(propertyName, propertyValue));
                addedPropertTablePanel.setVisible(true);
                ((DefaultTableModel) addedPropertyTable.getModel()).setNumRows(propeties.size());
                for (int i = 0; i < propeties.size(); i++) {
                    addedPropertyTable.getModel().setValueAt(propeties.get(i).getName(), i, 0);
                    addedPropertyTable.getModel().setValueAt(propeties.get(i).getValue(), i, 1);
                }
//            for(int i =0 ; i<propeties.size();i++){
////               addedPropertyTable.getModel().setValueAt(propeties.get(i).getValue(), i, 1);
//                           
//            }
            }
        }


        addCustomPropertyNameTextField.setText("");
        addCustomPropertValueTextField.setText("");

    }//GEN-LAST:event_addAnotherProepertyButtonActionPerformed

    private void addCustomProeprtyOkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCustomProeprtyOkButtonActionPerformed
        //if (propeties.isEmpty() && addCustomPropertValueTextField.getText() != null) {

        String propertyName = addCustomPropertyNameTextField.getText();
        String propertyValue = addCustomPropertValueTextField.getText();

        if (!propeties.isEmpty()) {
            if (propertyName.equals("") || propertyValue.equals("")) {
                this.dispose();
//            JOptionPane.showMessageDialog(this, "Please enter both property name and value");
            } else if (!propertyName.equals("") && !propertyValue.equals("")) {
                if (!propertyName.equals("fileName")) {
                    propeties.add(new BasicNameValuePair(propertyName, propertyValue));
                }

                this.dispose();

            } /*else if(!propeties.isEmpty() && addCustomPropertValueTextField.getText() != null){
            String propertyName = addCustomPropertyNameTextField.getText();
            String propertyValue = addCustomPropertValueTextField.getText();
            
            propeties.add(new BasicNameValuePair(propertyName, propertyValue));
            this.dispose();
            }*/ else {
                this.dispose();
            }
        } else {
            if (propertyName.equals("") || propertyValue.equals("")) {
//                this.dispose();
            JOptionPane.showMessageDialog(this, "Please enter both property name and value");
            } else if (!propertyName.equals("") && !propertyValue.equals("")) {
                if (!propertyName.equals("fileName")) {
                    propeties.add(new BasicNameValuePair(propertyName, propertyValue));
                }

                this.dispose();

            } /*else if(!propeties.isEmpty() && addCustomPropertValueTextField.getText() != null){
            String propertyName = addCustomPropertyNameTextField.getText();
            String propertyValue = addCustomPropertValueTextField.getText();
            
            propeties.add(new BasicNameValuePair(propertyName, propertyValue));
            this.dispose();
            }*/ else {
                this.dispose();
            }
        }

        // TODO add your handling code here:
    }//GEN-LAST:event_addCustomProeprtyOkButtonActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        this.dispose();
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed

    public List<NameValuePair> getAddedProperties() {
        return propeties;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Property().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addAnotherProepertyButton;
    private javax.swing.JButton addCustomProeprtyOkButton;
    private javax.swing.JTextField addCustomPropertValueTextField;
    private javax.swing.JTextField addCustomPropertyNameTextField;
    private javax.swing.JPanel addedPropertTablePanel;
    private javax.swing.JTable addedPropertyTable;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
